package com.zp.qualitypractice.list.me;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;

import com.zp.qualitypractice.list.header.BaseHeader;

/**
 * Created by izp on 2017/9/6.
 */

public class RecyclerHelper {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private BaseHeader mHeader;
    private float mLastY = -1;

    public RecyclerHelper(RecyclerView mRecyclerView, RecyclerView.Adapter mAdapter) {
        this.mRecyclerView = mRecyclerView;
        this.mAdapter = mAdapter;

    }

    private void addTouchEvent(){
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (mLastY == -1) {
                    mLastY = motionEvent.getRawY();
                }

                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mLastY = motionEvent.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        final float deltaY = motionEvent.getRawY() - mLastY;
                        mLastY = motionEvent.getRawY();
                        // 如果列表中第一个Item是可见的, 且Header的部分可见或者向下滑动，则动态设置Header高度
                        if (((LinearLayoutManager)mRecyclerView.getLayoutManager()).findFirstVisibleItemPosition()==0
//                                // 只有LayoutManager才有查找第一个和最后一个可见view位置的方法，这里使用的是LinearLayoutManger
                                && (mHeader.getHeight() > 0 || deltaY > 0)) {
//                            mHeader.updateHeight(deltaY * OFFSET_RADIO);
                            return true;
                        }
                        break;
                    default:
                        mLastY = -1;
                        // 用户松开手时候，如果列表第一个Item可以见
//                        if (getFirstVisiblePosition() == 0) {
//                            // 如果Header展示的高度大于Header的真正高度，则可刷新
//                            if (mHeader.getShowHeight()  > mHeaderViewHeight) {
//                                mPullRefreshing = true;
//                                mHeader.updateText(R.string.ptr_refresh_refreshing);
//                                mHeader.startLoading();
//                                if (mRefreshListener != null) {
//                                    mRefreshListener.onRefresh();
//                                }
//                            }
//                            // 根据当前情况重置Header高度
//                            resetHeaderHeight();
//                            return true;
//                        }
                        break;
                }
                return mRecyclerView.onTouchEvent(motionEvent);
            }
        });
    }

}
